package cn.alusiaor.commonutil;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Pictures;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import wiki.xsx.core.pdf.doc.XEasyPdfDocument;
import wiki.xsx.core.pdf.handler.XEasyPdfHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

/**
 * @Author: zhuzhiping
 * @Description:
 * @Date: 2020/11/12 18:32
 */

public class PDFUtil {

    public static void main(String[] args) throws Exception {
        String OUTPUT_PATH = "C:\\Users\\zhuzhiping\\Desktop\\";
        String sourcePath = OUTPUT_PATH + "99.pdf";
        String filePath = OUTPUT_PATH + "666.pdf";
        String imagePath = OUTPUT_PATH + "8888.png";
        String FONT_PATH = "C:\\Windows\\Fonts\\simfang.ttf";

        XEasyPdfDocument build = XEasyPdfHandler.Document.build(sourcePath);
        PDRectangle pageSize = build.getPageList().get(0).getParam().getPageSize();

        build.addPage(
                XEasyPdfHandler.Page.build(pageSize,
                        XEasyPdfHandler.Text.build(12f, DateTimeFormatter.ofPattern("yyyy年MM月dd日").format(LocalDate.now()))
                                .setPosition(20F, 40F),
                        XEasyPdfHandler.Image.build(new File(imagePath))
                                .setHeight(50)
                                .setWidth(50)
                                .setPosition(20F, 50F)
                )
        ).setFontPath(FONT_PATH).save(filePath);

        addZhang();
    }


    public static void addZhang() throws IOException {

        XWPFTemplate template = XWPFTemplate.compile("template.docx").render(
                new HashMap<String, Object>() {{
                    put("zhang", Pictures.ofLocal("20210119115700.png").create());
                }});
        template.writeAndClose(new FileOutputStream("output.docx"));

    }


}
